﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using StudentManagement.Models;

namespace StudentManagement
{
    public class Startup
    {
        public  Startup()
        {

        }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddSingleton<IStudentRepository, MockStudentRepository>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env , ILogger<Startup> logger )
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //下面三行设置meng.html为默认静态文件,优先于index.html等
            FileServerOptions fileServerOptions = new FileServerOptions();
            fileServerOptions.DefaultFilesOptions.DefaultFileNames.Clear();
            fileServerOptions.DefaultFilesOptions.DefaultFileNames.Add("meng.html");
            
            app.UseFileServer(fileServerOptions);    //它等于app.UseDefaultFiles();与app.UseStaticFiles();之和

            /*
            //添加默认文件中间件,如index.html index.htm default.html default.htm
            app.UseDefaultFiles();
            //添加静态文件中间件
            app.UseStaticFiles();
            */
            app.UseMvcWithDefaultRoute();
            
            app.Run(async (context) =>
            {
                logger.LogInformation("处理请求");
                //throw new Exception("error"); //抛出异常，进入异常界面
                await context.Response.WriteAsync("nihaoa");
                
            });
        }
    }
}
